// pages/CardRoll/index.js
var util = require('../../utils/util.js');
var API = require('../../config/api.js');

import Service from '../../servers/pay';
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    List:[
      {
        image:'',
        title:'阳光卡',
        price:298,
      },
      {
        image:'',
        title:'日月卡',
        price:398,
      },
      {
        image:'',
        title:'月光卡',
        price:496,
      },
      {
        image:'',
        title:'小时卡',
        price:8,
      },
      {
        image:'',
        title:'月光卡',
        price:496,
      }
    ]
  },
  addCart:function(e){
    var dealId = e.currentTarget.dataset.id;
    var title =  e.currentTarget.dataset.title;
    var bind = e.currentTarget.dataset.bind;
    console.log('addCard', e.currentTarget.dataset);
    var self=this;
    Dialog.confirm({
      title:"确认购买",
      message:"您将购买"+ title
    }).then(function(){
      console.log('123123123123123');
      self.prepayDeal(dealId, bind);
    }).catch(function(){
      Toast('好的,再想想!');
    });
  },
  prepayDeal: function(did, cid){
    Service.buyDeal(did, cid).then(res=>{
      console.log("success", res);
      Notify({
        type:'success',
        message:'您购买的卡券请到“我的“->”我的卡券“查看'
      })
    }).catch(error=>{
      console.log("error",error);
    });
  },
  GetList(){
    var self = this;
    util.request(
      API.CouponList, {'shopId': 2}, "GET").then(
        res=>{
          if( res.result == "error"){
            Toast(res.message);
            return;
          }

          console.log('res********************',res);
          self.setData({
            List: res.data
          });
        }
      )
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.GetList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})